﻿Imports System.Net.Mail
Imports System.Data
Imports System.Data.SqlClient
Partial Class notauthenticated
    Inherits System.Web.UI.Page


    Protected Sub resend_mail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles resend_mail.Click

        Dim myUser As MembershipUser = Membership.GetUser(Request.Params("user"))

        Dim fname As String = Me.firstName
        Dim myemail As String = "jrb9249@gmail.com" ' me.univ_email


        Dim mail As MailMessage = New MailMessage()
        Dim myGuid As Guid = CType(myUser.ProviderUserKey, Guid)



        mail.To.Add(myemail) 'change this to email variable
        mail.From = New MailAddress("donotreply@freestudentexchange.com")
        mail.Subject = "Email Verification - Free Student Exchange"

        Dim Body As String = fname & ", <br><br> <p>This email is to confirm your Free Student Exchange account created for " & myemail & ". <br>" & _
        "<br><a href='http://localhost:54164/Free%20Student%20Exchange%203/confirm.aspx?id=" & myGuid.ToString() & "'>Click here to " & _
        "verify your email address</a></p><br><br><p>If you are having trouble clicking the above link, copy the following link and paste " & _
        "it into your browser's address bar:<br><br>http://localhost:54164/Free%20Student%20Exchange%203/confirm.aspx?id=" & myGuid.ToString() & _
        "</p><br><hr><p style='font-size:10;color:#787b7c;'>If you have any questions, please visit our help page at " & _
        "<a href='http://www.freestudentexchange.com/help'>www.FreeStudentExchange.com/Help.aspx</a> or contact us at feedback@freestudentexchange.com</p>"

        mail.Body = Body
        mail.IsBodyHtml = True

        Dim smtp As SmtpClient = New SmtpClient()
        smtp.Host = "smtp.gmail.com"
        smtp.Port = 587
        smtp.Credentials = New System.Net.NetworkCredential("fse.4testing9249@gmail.com", "fsetesting435")
        smtp.EnableSsl = True
        smtp.Send(mail)

        main_label.Visible = False
        resent_label.Visible = True
        resend_link_block.Visible = False
        home_link_block.Visible = True

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Request.IsAuthenticated Then
            Response.Redirect("home.aspx")
        End If
        
        If Not IsPostBack Then
            If Not Request.Params("user") Is Nothing And Not Session("userID") Is Nothing Then
                ViewState.Add("userID", Session("userID"))
                Session.Remove("userID")
                Dim userName As String = Request.Params("user")
                Dim myUser As MembershipUser = Membership.GetUser(userName)
                If myUser.ProviderUserKey <> ViewState("userID") Then
                    MsgBox("GUID does not match viewstate userID")
                    Response.Redirect("home.aspx")
                Else
                    Dim myguid As Guid = CType(ViewState("userID"), Guid)
                    Me.userName = Request.Params("user").ToString
                    If getUserdata(myguid) = False Then
                        MsgBox("There was an error connecting to the server.")
                    End If
                End If
            Else
                Response.Redirect("home.aspx")
            End If
        End If
    End Sub

  

    Protected Function getUserdata(ByRef Username As Guid) As Boolean

        Dim membershipConnectionString As String = ConfigurationManager.ConnectionStrings("FSEmembershipConnection").ConnectionString

        Dim conn As New SqlConnection(membershipConnectionString)
        Dim query1 As String = "Select m.[Email], p.firstname [FirstName] " & _
            "from aspnet_Membership m inner join UserProfiles p on p.UserId = m.UserId where (m.[UserId]=@myUsername)"
        Dim cmd As New SqlCommand(query1, conn)
        Dim email As String, fname As String

        email = ""
        fname = ""

        Dim myUsername As New SqlParameter("@myUsername", SqlDbType.UniqueIdentifier)
        myUsername.Value = Username
        cmd.Parameters.Add(myUsername)

        Try

            conn.Open()
            Dim myReader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult)

            While myReader.Read()
                email = myReader("Email").ToString
                fname = myReader("FirstName").ToString
            End While

            Me.firstName = fname
            Me.univ_email = email

            conn.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try



    End Function

    Private Property firstName() As String
        Get
            Return ViewState("firstName")
        End Get
        Set(ByVal value As String)
            If ViewState("firstName") Is Nothing Then
                ViewState.Add("firstName", value)
            Else
                ViewState("firstName") = value
            End If

        End Set
    End Property

    Private Property userName() As String
        Get
            Return ViewState("userName")
        End Get
        Set(ByVal value As String)
            If ViewState("userName") Is Nothing Then
                ViewState.Add("userName", value)
            Else
                ViewState("userName") = value
            End If

        End Set
    End Property

    Private Property univ_email() As String
        Get
            Return ViewState("univ_email")
        End Get
        Set(ByVal value As String)
            If ViewState("univ_email") Is Nothing Then
                ViewState.Add("univ_email", value)
            Else
                ViewState("univ_email") = value
            End If

        End Set
    End Property

End Class
